﻿
using System;

namespace SharpDepend.Datatypes
{
    public struct Vector4
    {
        public static readonly Vector4 Default = new Vector4();
        public static readonly Vector4 Zero = new Vector4();
        public static readonly Vector4 ZeroOne = new Vector4(0, 0, 1, 1);
        public static readonly Vector4 MaxRectanlge = new Vector4(double.MinValue, double.MinValue, double.MaxValue, double.MaxValue);
        public static readonly Vector4 MaxRectanlgeInverted = new Vector4(double.MaxValue, double.MaxValue, double.MinValue, double.MinValue);

        public double X1;
        public double Y1;
        public double X2;
        public double Y2;

        /// <summary>
        /// Get or set the X.
        /// </summary>
        public double X
        {
            get { return X1; }
            set
            {
                double tempWidth = Width;
                X1 = value;
                Width = tempWidth;
            }
        }

        /// <summary>
        /// Get or set the Y.
        /// </summary>
        public double Y
        {
            get { return Y1; }
            set
            {
                double tempHeight = Height;
                Y1 = value;
                Height = tempHeight;
            }
        }

        /// <summary>
        /// Get or set the width.
        /// </summary>
        public double Width { get { return X2 - X1; } set { X2 = X1 + value; } }

        /// <summary>
        /// Get or set the height.
        /// </summary>
        public double Height { get { return Y2 - Y1; } set { Y2 = Y1 + value; } }

        public double CenterX { get { return X + Width * 0.5; } }

        public double CenterY { get { return Y + Height * 0.5; } }

        public Vector4(double x1, double y1, double x2, double y2)
        {
            X1 = x1;
            Y1 = y1;
            X2 = x2;
            Y2 = y2;
        }

        /// <summary>
        /// If width or/and height is negative, it will be swapped.
        /// </summary>
        public void SwapNegativeDimensions()
        {
            if (X1 > X2)
            {
                double tmp = X1;
                X1 = X2;
                X2 = tmp;
            }
            if (Y1 > Y2)
            {
                double tmp = Y1;
                Y1 = Y2;
                Y2 = tmp;
            }
        }

        public static Vector4 operator +(Vector4 left, Vector4 right)
        {
            return new Vector4(left.X1 + right.X1, left.Y1 + right.Y1, left.X2 + right.X2, left.Y2 + right.Y2);
        }

        public static Vector4 operator -(Vector4 left, Vector4 right)
        {
            return new Vector4(left.X1 - right.X1, left.Y1 - right.Y1, left.X2 - right.X2, left.Y2 - right.Y2);
        }

        public static Vector4 operator *(Vector4 left, double right)
        {
            return new Vector4(left.X1 * right, left.Y1 * right, left.X2 * right, left.Y2 * right);
        }

        public static Vector4 operator /(Vector4 left, double right)
        {
            return new Vector4(left.X1 / right, left.Y1 / right, left.X2 / right, left.Y2 / right);
        }

        public bool IsInside(Vector2 point)
        {
            return point.X > X1 && point.X < X2 &&
                point.Y > Y1 && point.Y < Y2;
        }
    }
}
